﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using UIT.ScrumTool.DTO;
using UIT.ScrumTool.Service.Interface;
using UIT.ScrumTool.Service.Model;
using UIT.ScrumTool.Service.Utilities;

namespace UIT.ScrumTool.Service.Business
{
    /// <summary>
    /// Cac phuong thuc ma nguoi dung co the su dung
    /// </summary>
    public partial class ControlService : IUserService
    {
        // ket noi voi linq
        DataScrumToolDataContext dataContext = new DataScrumToolDataContext();

        //Phuong thuc lay danh sach nguoi dung
        public List<UserDto> UserGetByAll()
        {
            List<UserDto> lsuersDto = new List<UserDto>();
            List<User> ls = dataContext.sp_Users_GetByAll().ToList();
            foreach (var user in ls)
            {
                UserDto userDto = new UserDto();
                userDto.Id = user.Id;
                userDto.LoginName = user.LoginName;
                userDto.FistName = user.FistName;
                userDto.LastName = user.LastName;
                userDto.Password = user.Password;
                userDto.Email = user.Email;
                userDto.IMType = user.IMType;
                userDto.IM = user.IM;
                userDto.Picture = null;
                userDto.CompanyPostion = user.CompanyPosition;
                userDto.Profile = user.Profile;
                lsuersDto.Add(userDto);
            }
            return lsuersDto;
        }

        public UserDto UserGetById(int id)
        {
            try
            {
                return UserConvertUserDto(dataContext.sp_Users_GetById(id).Single());
            }
            catch (Exception)
            {
                return null;
            }
        }


        //phuong thuc insert
        public void UserInsert(UserDto userDto)
        {
            dataContext.sp_Users_Insert(userDto.LoginName, userDto.Password, userDto.LastName, userDto.FistName, userDto.Email,
                                       userDto.IMType, userDto.IM, userDto.Picture, userDto.CompanyPostion,
                                       userDto.Profile);
        }

        //phuong thuc delete user
        public bool UserDelete(int id)
        {
            try
            {
                dataContext.sp_Users_Delete(id);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        //phuong thuc update user
        public bool UserUpdate(UserDto userDto)
        {
            try
            {
                dataContext.sp_Users_Update(userDto.Id, userDto.LoginName, userDto.Password, userDto.FistName,
                                        userDto.LastName, userDto.Email, userDto.IMType, userDto.IM, userDto.Picture,
                                        userDto.CompanyPostion, userDto.Profile);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static UserDto UserConvertUserDto(User user)
        {
            UserDto userDto = new UserDto();
            userDto.Id = user.Id;
            userDto.LoginName = user.LoginName;
            userDto.FistName = user.FistName;
            userDto.LastName = user.LastName;
            userDto.Password = user.Password;
            userDto.IM = user.IM;
            userDto.IMType = user.IMType;
            userDto.Email = user.Email;
            userDto.Picture = user.Picture;
            userDto.Profile = user.Profile;
            userDto.CompanyPostion = user.CompanyPosition;
            return userDto;
        }
    }
}
